//
//  TMFindOrRegiserController.m
//  国学商家管理
//
//  Created by mac on 2018/6/19.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMFindOrRegiserController.h"
#import "SPToolSet.h"
#import "TMUserManager.h"
@interface TMFindOrRegiserController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *identifyCodeTf;
@property (weak, nonatomic) IBOutlet UIButton *identifyBtn;
@property (weak, nonatomic) IBOutlet UITextField *passwordTf;
@property (weak, nonatomic) IBOutlet UITextField *againPasswordTf;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (nonatomic, strong) NSTimer *countDownTimer;
@property (nonatomic, assign) NSInteger timestamp;
//邀请码
@property (weak, nonatomic) IBOutlet UIImageView *invaImage;
@property (weak, nonatomic) IBOutlet UITextField *incatinTf;
@property (weak, nonatomic) IBOutlet UIView *incaView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConst;

@end

@implementation TMFindOrRegiserController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    if (self.isRegisterVC == YES) {
//        [self.identifyBtn startWithKey:@"register"];
//    }else{
//        [self.identifyBtn startWithKey:@"find"];
//    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.isRegisterVC == YES) {
        self.title = @"注册";
    }else{
        self.title = @"忘记密码";
        [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        self.incaView.hidden = YES;
        self.incatinTf.hidden = YES;
        self.invaImage.hidden = YES;
        self.topConst.constant = 42;
        
    }
    //注册 和找回密码
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textChange: ) name:UITextFieldTextDidChangeNotification
                                               object:nil];
    self.phoneTf.delegate = self;
    self.passwordTf.delegate = self;
    self.againPasswordTf.delegate = self;
    self.incatinTf.delegate = self;
}
- (IBAction)loginBtnClick:(UIButton *)sender {
    if (self.phoneTf.text.length != 11 || !self.identifyCodeTf.text || !self.passwordTf.text ||!self.againPasswordTf.text) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的内容！"];
        return;
    }
    if (![SPToolSet checkPassword:self.passwordTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入6-18位字母数字组合密码！"];
        return;
    }
    if (![SPToolSet checkPassword:self.againPasswordTf.text]) {
        [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入6-18位字母数字组合密码！"];
        return;
    }
    if (self.isRegisterVC == YES) {
        //用户名？
        [TMUserManager registerWithDictionary:@{@"phone":self.phoneTf.text,@"verify":self.identifyCodeTf.text,@"password":self.passwordTf.text,@"re_password":self.againPasswordTf.text} Success:^(id respon) {
            [TMUserManager loginWithUserName:self.phoneTf.text password:self.passwordTf.text successed:^(id index){
                [TMUserManager displayRCConfings];
                UIWindow *window = [UIApplication sharedApplication].keyWindow;
                TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
                window.rootViewController = rootTabbar;
        }];
    }];
    }else{
        [TMUserManager findPasswordWithDictionary:@{@"phone":self.phoneTf.text,@"verify":self.identifyCodeTf.text,@"password":self.passwordTf.text,@"re_password":self.againPasswordTf.text,@"invite_code":self.incatinTf.text} Success:^(id respon) {
            [TMUserManager loginWithUserName:self.phoneTf.text password:self.passwordTf.text successed:^(id index){
                [TMUserManager displayRCConfings];
                UIWindow *window = [UIApplication sharedApplication].keyWindow;
                TMBaseTabbarContriller *rootTabbar = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"TMBaseTabbarContriller"];
                window.rootViewController = rootTabbar;
            }];
        }];
    }
}
- (IBAction)identifyCodeBtnClick:(UIButton *)sender{
    if (self.phoneTf.text.length == 11) {
        if (self.isRegisterVC == YES) {
            [self.identifyBtn startTimer:60 andKey:@"register"];
        }else{
            [self.identifyBtn startTimer:60 andKey:@"find"];
        }
        [TMUserManager verificationCodeSendWithDictionary:self.phoneTf.text];
    }else{
        [TMProgressHUD showErrorWithStatus:@"请输入正确手机号"];
    }
}
//正则表达式验证手机号
- (BOOL)validatePhoneNumber:(NSString *) textString
{
    NSString* number=@"^1[3|4|5|7|8][0-9]\\d{8}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}
- (BOOL)inputShouldLetterOrNumWithText:(NSString *)inputString {
    if (inputString.length == 0) return NO;
    NSString *regex =@"[a-zA-Z0-9]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pred evaluateWithObject:inputString];
}



- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == self.phoneTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.phoneTf.text.length >= 11) {
            self.phoneTf.text = [textField.text substringToIndex:11];
            return NO;
        }
    }
    if (textField == self.passwordTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.passwordTf.text.length >= 18 ) {
            self.passwordTf.text = [textField.text substringToIndex:18];
            return NO;
        }
    }
    if (textField == self.againPasswordTf) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (self.againPasswordTf.text.length >= 18 ) {
            self.againPasswordTf.text = [textField.text substringToIndex:18];
            return NO;
        }
    }
    return YES;
}

- (void)textChange:(UITextField *)tf{
//    if (tf == self.passwordTf ) {
//        if (![self inputShouldLetterOrNumWithText:self.passwordTf.text]) {
//            [TMProgressHUD showErrorWithStyleDarkStatus:@"请输入正确的密码格式"];
//            return;
//        }
//    }
//    if (tf == self.againPasswordTf ) {
//        if (![self inputShouldLetterOrNumWithText:self.againPasswordTf.text]) {
//            return;
//        }
//    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

@end
